//
//  MessageAcceptViewController.m
//  BGM
//
//  Created by 我要变好看 on 2018/12/18.
//  Copyright © 2018 com.alirtc.demo. All rights reserved.
//

#import "MessageAcceptViewController.h"

#import "PushMessageInfoViewController.h"

#import "UserInfoTableViewCell.h"
#import "MageRelationShipTableViewCell.h"
#import "Common.h"
#import "BGMHTTP.h"
#import "BGMUser.h"
#import "ChatViewController.h"
#import "FriendViewController.h"


@interface MessageAcceptViewController ()<UITableViewDelegate,UITableViewDataSource,MageRelationShipTableViewCellDelegate>

@property (nonatomic, strong)UITableView *myTable;
@property (nonatomic, strong)NSDictionary *accpetDic;
@property (nonatomic, strong)UIButton *bottomBtn;
@property (nonatomic, strong)NSString *groupId;

@property (nonatomic, strong)NSArray *relationShipArray;


@end

@implementation MessageAcceptViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navview.hidden = NO;
    self.titleBtn.hidden = NO;
    CGRect rect = self.titleBtn.frame;
    rect.size.width = rect.size.width+70;
    self.titleBtn.frame = rect;
    if(self.isCommon){
        [self.titleBtn setTitle:@"评论通知" forState:UIControlStateNormal];
    }else{
        [self.titleBtn setTitle:@"受理通知" forState:UIControlStateNormal];
    }
     self.navRightBtn.hidden = NO;
    [self.navRightBtn setTitle:@"详情" forState:UIControlStateNormal];
    [self.navRightBtn setTitleColor:bgmgrayColor forState:UIControlStateNormal];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.myTable = [[UITableView alloc]initWithFrame:CGRectMake(0, self.navview.frame.size.height, SCREENW, SCREENH-self.navview.frame.size.height-150)];
    self.myTable.delegate = self;
    self.myTable.dataSource = self;
    self.myTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.myTable];
    
    
    self.bottomBtn = [[UIButton alloc]initWithFrame:CGRectMake(30, SCREENH-120, SCREENW-60, 50)];
    self.bottomBtn.layer.cornerRadius = 8;
    self.bottomBtn.layer.masksToBounds = YES;
    self.bottomBtn.backgroundColor = bgmyelloeColor;
    [self.bottomBtn setTitleColor:bgmgrayColor forState:UIControlStateNormal];
    [self.view addSubview:self.bottomBtn];
    [self.bottomBtn addTarget:self action:@selector(clickBottom) forControlEvents:UIControlEventTouchUpInside];
    
    if(!self.isCommon){
        self.bottomBtn.hidden = NO;
        [self requestAcceptInfo];
    }else{
        self.bottomBtn.hidden = YES;
        [self requestCommonInfo];

    }
    
}

- (void)requestCommonInfo{
    NSDictionary *param = @{@"userToken":[BGMUser shareInstance].userToken,@"pushMessageId":self.pushMessageId};
    [BGMHTTP requestWithMethodName:@"message/getCommentDetail" parameters:param completion:^(BOOL success, NSError *error, NSDictionary *result) {
        dispatch_async(dispatch_get_main_queue(), ^{
//            self.accpetDic = [result objectForKey:@"data"];
//            self.groupId = [NSString stringWithFormat:@"%@",[self.accpetDic objectForKey:@"groupID"]];
//            [self.myTable reloadData];
            
//            [self changeMessageStatus];

        });
    }];
}
- (void)requestAcceptInfo{
    
    NSDictionary *param = @{@"userToken":[BGMUser shareInstance].userToken,@"pushMessageId":self.pushMessageId};
    [BGMHTTP requestWithMethodName:@"message/acceptancePushDetail" parameters:param completion:^(BOOL success, NSError *error, NSDictionary *result) {
       
        dispatch_async(dispatch_get_main_queue(), ^{
            self.accpetDic = [result objectForKey:@"data"];
            self.groupId = [NSString stringWithFormat:@"%@",[self.accpetDic objectForKey:@"groupID"]];
            if (self.groupId.length ==0) {
                [self.bottomBtn setTitle:@"发起会话" forState:UIControlStateNormal];
            }else{
                [self.bottomBtn setTitle:@"进入会话" forState:UIControlStateNormal];
            }
            self.relationShipArray = [self.accpetDic objectForKey:@"humanChain"];
            [self.myTable reloadData];
        });
    }];
}




- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return 80;
    }else if (indexPath.row ==1){
        return 110;
    }else{
        return 100;
    }
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.isCommon?2:3;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row ==0) {
        static NSString *identifier = @"UserInfoTableViewCell";
        UserInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[UserInfoTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        [cell setUserInfo:self.accpetDic];
        return cell;
    }
    else if (indexPath.row ==1){
        if(self.isCommon){
            static NSString *identifier = @"acceptCommonCell";
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
            if (cell == nil) {
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
                UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(30, 0, 200, 40)];
                label.text = @"留言";
                label.textColor = bgmgrayColor;
                label.font = [UIFont systemFontOfSize:13];
                [cell addSubview:label];
                
                UILabel *line = [[UILabel alloc]initWithFrame:CGRectMake(30, 0, SCREENW, 0.3)];
                line.backgroundColor = [UIColor grayColor];
                line.alpha = 0.3;
                [cell addSubview:line];
                
                UILabel *commont = [[UILabel alloc]initWithFrame:CGRectMake(30, 40, 200, 200)];
                commont.textColor = bgmmainColor;
                commont.font = [UIFont systemFontOfSize:14];
                commont.numberOfLines = 0;
                [cell addSubview:commont];
                commont.tag = 100;
            }
            
            if(self.accpetDic.count>0){
                UILabel *commont = (UILabel *)[cell viewWithTag:100];
                //            commont.text = ;
                NSString *message = [NSString stringWithFormat:@"%@",[self.accpetDic objectForKey:@"message"]];
                UIFont * tfont = [UIFont systemFontOfSize:14];
                NSDictionary * tdic = [NSDictionary dictionaryWithObjectsAndKeys:tfont,NSFontAttributeName,nil];
                CGSize  actualsize =[message boundingRectWithSize:CGSizeMake(SCREENW-50, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin  attributes:tdic context:nil].size;
                commont.frame = CGRectMake(30, 40, actualsize.width, actualsize.height);
                commont.text = message;
                
            }
            
            return cell;
        }
        else{
            static NSString *identfier = @"MageRelationShipTableViewCell";
            MageRelationShipTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identfier];
            if (cell == nil) {
                cell = [[MageRelationShipTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identfier];
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                cell.shipDelegate = self;
                
            }
            
//            [cell shipView:self.relationShipArray];
            return cell;
            
        }
    }else{
        static NSString *identifier = @"acceptCommonCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(30, 0, 200, 40)];
            label.text = @"评论";
            label.textColor = bgmgrayColor;
            label.font = [UIFont systemFontOfSize:13];
            [cell addSubview:label];
            
            UILabel *line = [[UILabel alloc]initWithFrame:CGRectMake(30, 0, SCREENW, 0.3)];
            line.backgroundColor = [UIColor grayColor];
            line.alpha = 0.3;
            [cell addSubview:line];
            
            UILabel *commont = [[UILabel alloc]initWithFrame:CGRectMake(30, 40, 200, 200)];
            commont.textColor = bgmmainColor;
            commont.font = [UIFont systemFontOfSize:14];
            commont.numberOfLines = 0;
            [cell addSubview:commont];
            commont.tag = 100;
        }
        
        if(self.accpetDic.count>0){
            UILabel *commont = (UILabel *)[cell viewWithTag:100];
            //            commont.text = ;
            NSString *message = [NSString stringWithFormat:@"%@",[self.accpetDic objectForKey:@"message"]];
            UIFont * tfont = [UIFont systemFontOfSize:14];
            NSDictionary * tdic = [NSDictionary dictionaryWithObjectsAndKeys:tfont,NSFontAttributeName,nil];
            CGSize  actualsize =[message boundingRectWithSize:CGSizeMake(SCREENW-50, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin  attributes:tdic context:nil].size;
            commont.frame = CGRectMake(30, 40, actualsize.width, actualsize.height);
            commont.text = message;
            
        }
        
        return cell;
        
    }
        
        
     
    
}

- (void)clickRight{
    if(self.accpetDic.count>0){
        PushMessageInfoViewController *vc = [[PushMessageInfoViewController alloc]init];
        vc.messageId = [NSString stringWithFormat:@"%@",[self.accpetDic objectForKey:@"publishId"]];
        vc.helpUsersId =[NSString stringWithFormat:@"%@",[self.accpetDic objectForKey:@"helpUsersId"]];
        vc.isAccept = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (void)clickBottom{
    if(self.accpetDic.count>0){
        if (self.groupId.length ==0) {  //
            NSArray *array = [self.accpetDic objectForKey:@"humanChain"];
            NSString *friends = @"";
            if (array.count == 2) {
                friends = @"";
            }else{
                
            }
            
            NSDictionary *param = @{@"userToken":[BGMUser shareInstance].userToken,
                                    @"usersId":[BGMUser shareInstance].userId,
                                    @"friendsId":friends,
                                    @"accpId":[NSString stringWithFormat:@"%@",[self.accpetDic objectForKey:@"aUsersId"]],
                                    @"publishId":[NSString stringWithFormat:@"%@",[self.accpetDic objectForKey:@"publishId"]]};
            [BGMHTTP requestWithMethodName:@"message/creatGroup" parameters:param completion:^(BOOL success, NSError *error, NSDictionary *result) {

                dispatch_async(dispatch_get_main_queue(), ^{
                    NSString *code =  [NSString stringWithFormat:@"%@",[result objectForKey:@"code"]];
                    if([code isEqualToString:@"200"]){
                        NSString *data =  [NSString stringWithFormat:@"%@",[result objectForKey:@"data"]];
                        self.groupId = data;
                        ChatViewController *vc = [[ChatViewController alloc]init];
                        vc.isGroup = YES;
                        vc.groupId = self.groupId;
                        [self.navigationController pushViewController:vc animated:YES];
                    }
       
                });

            }];
        }
        else{
            ChatViewController *vc = [[ChatViewController alloc]init];
            vc.isGroup = YES;
            vc.groupId = self.groupId;
            vc.messageInfo = self.accpetDic;
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
    
    
    
}
-(void)findFriendInfo:(int)i{
    if (i < self.relationShipArray.count-1) {
        NSDictionary *dic = [self.relationShipArray objectAtIndex:i];
        FriendViewController *vc = [[FriendViewController alloc]init];
//        vc.hidesBottomBarWhenPushed = YES;
        vc.userId = [BGMUser shareInstance].userId;
        vc.userToken = [BGMUser shareInstance].userToken;
        vc.chUsersId = [NSString stringWithFormat:@"%@",[dic objectForKey:@"usersId"]];
        vc.otherImage = [NSString stringWithFormat:@"%@",[dic objectForKey:@"userHead"]];
        vc.otherName = [NSString stringWithFormat:@"%@",[dic objectForKey:@"userName"]];
        [self.navigationController pushViewController:vc animated:YES];
    }
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
